// pages/repayBook/repayBook.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    display: 'none',
    repayBooks: [],
    repayBookIdArr: [],
    disabled: true,
    imgUrl:api.RootUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    util.request(api.getBorrowList,{status:1}).then(function(res){
        if (res.data.borrowList.length == 0) {
          console.log('0000');
          that.setData({
            display: 'block'
          })
        } else {
          console.log('1111');
          for (var i = 0; i < res.data.borrowList.length; i++) {
            console.log(i);
            console.log(res.data.borrowList[i].bookName);
            res.data.borrowList[i].endTime = util.formatTime(res.data.borrowList[i].endTime);
            res.data.borrowList[i].startTime = util.formatTime(res.data.borrowList[i].startTime);
            if (res.data.borrowList[i].bookName.length > 30) {
              res.data.borrowList[i].bookName = res.data.borrowList[i].bookName.slice(0, 29) + '...';
              that.setData({
                repayBooks: res.data.borrowList
              });
            } else {
              that.setData({
                repayBooks: res.data.borrowList
              });
            }
          }
        }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkboxChange: function (e) {
    if (e.detail.value.length < 1) {
      this.setData({
        disabled: true
      });
    } else {
      this.setData({
        disabled: false,
        BorrowIdArr: e.detail.value
      });
    }
    console.log(e.detail.value);
  },

  createRepayQrCode: function () {
    var borrowId = this.data.BorrowIdArr.join(',');
    var title = "我的还书二维码";
    wx.navigateTo({
      url: '../book-return/book-return?borrowId=' + borrowId,
    })
  },
  /* 续借 */
  borrowAgainBtn: function (e) {
    var that = this;
    var isbn = e.currentTarget.dataset.isbn;
    var id = e.currentTarget.dataset.id;
    console.log(e.currentTarget.dataset.isbn + e.currentTarget.dataset.id);
    var phonenumber = getApp().data.phonenumber;
    wx.request({
      url: 'https://www.kyssky.party:8443/weixin/BookForXujie',
      data: {
        ISBN: isbn,
        ID: id,
        phonenumber: phonenumber
      },
      success: function (res) {
        if (res.data.statusCode == '0') {
          wx.showToast({
            title: '续借成功',
          })
          that.onLoad();
        } else {
          wx.showToast({
            image: '../../images/timg.png',
            title: '续借失败 只能续借一次',
          })
          that.onLoad();
        }
      }
    })

  },
  gotoInfo: function (e) {
    var isbn = e.currentTarget.dataset.isbn;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../bookinfo/bookinfo?bookisbnandid=' + isbn + id,
    })
    console.log('跳转到书籍详情页');
  }
})